# -*- coding: utf-8 -*-

#This file is part of PyAero library.
#
# PyAero is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyAero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PyAero.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Guillaume Baty'
__copyright__ = u'Copyright 2011-2012 (C) andheo'
__credits__ = [u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

__all__ = ['GuiDataCreator']

from Qt4 import QtGui

from pyLot.qtgui import (ToolWidget, make_exporter, generate_pyfile_from_uifile)
from pyLot.core import get_data

from handler_data_creator import DataCreatorHandler

generate_pyfile_from_uifile(__name__)
from _gui_data_creator import Ui_GuiDataCreator
class GuiDataCreator(QtGui.QWidget, Ui_GuiDataCreator, ToolWidget):

  identifier = 'PyAero.toolwidget.data_creator'
  title = 'Data Creator'
  main_icon = get_data(__name__, u'icon.png')
  default_handler_class = DataCreatorHandler
  default_handler_kargs = {}


  def __init__(self):
    QtGui.QWidget.__init__(self)
    self.setupUi(self)
    ToolWidget.__init__(self)

    self._current = None
    self._pb_export = None

    self._cb_creators = []

    self.pb_display_creator = QtGui.QPushButton(u"Affichier l'éditor")

    self.layout_buttons.addWidget(self.pb_display_creator)

    self._createConnexions()
    self._fillCreatorCb()

  def _fillCreatorCb(self):
    self._cb_creators = []
    self.cb_creators.clear()

    for path in self.handler.getPaths(self.handler.creators, self.handler.fmts):
      self._cb_creators.append(path[0])
      self.cb_creators.addItem(self.handler.creators[path[0]].name)

  def _createConnexions(self):
    self.pb_display_creator.clicked.connect(self.actionDisplayCreator)

  def actionDisplayCreator(self):
    creator = self._cb_creators[self.cb_creators.currentIndex()]
    if self._current :
      self._current.hide()
    self._current = self.handler.creators[creator]
    self.layout_creator.addWidget(self._current)
    self._current.show()
    if self._pb_export :
      self.layout_buttons.removeWidget(self._pb_export)
      self._pb_export.close()
      del self._pb_export

    from IPython.Shell import IPShellQt4
    ipshell = IPShellQt4(user_ns=dict(widget=self, handler=self.handler))
    ipshell.start()

    self._pb_export = make_exporter(self._current.datarepr_id, self.handler)
    self.layout_buttons.addWidget(self._pb_export)
